package mint.modules.generator;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

import org.antlr.runtime.tree.Tree;

import mint.modules.symbol.Symbol;
import mint.modules.symbol.SymbolType;
import mint.modules.type.Type;
import mint.modules.type.TypeId;

public class FormalParameterNode implements MintNode {
	private String name;
	private Type type;
	private int line;
	private List<String> errorList;
	private Scope scope;
	
	public FormalParameterNode(Tree formalParameterTree, Scope theScope)
	{
		type = Type.getTypeFromTree(formalParameterTree.getChild(0));
		name = formalParameterTree.getChild(1).getText();
		line = formalParameterTree.getLine();
		errorList = new ArrayList<String>();
		scope = theScope;
	}
	
	public String getName()
	{
		return name;
	}
	
	public Type getType()
	{
		return type;
	}

	@Override
	public void testSemantic() {
		if (type.getTypeId() == TypeId.Object || type.getTypeId() == TypeId.ObjectArray)
		{
			Symbol symbol = scope.searchSymbol(type.getName(), scope.getContainerClass(), false);
			
			if (symbol == null)
				errorList.add(line + ": " + type.getName() + " doesn't exists on this scope");
			else
				if (symbol.getSymbolType() != SymbolType.Class)
					errorList.add(line + ": " + type.getName() + " isn't a type name");
		}
	}

	@Override
	public List<String> getSemanticErrors() {
		return new ArrayList<String>();
	}

	@Override
	public void emit(Formatter output) {
		output.format(type.emit());
	}

	@Override
	public int getLine() {
		return line;
	}

}
